//
//  FZAdsParamData.m
//  KidDubbing
//
//  Created by Victor Ji on 16/8/28.
//  Copyright © 2016年 Feizhu Tech. All rights reserved.
//

#import "FZAdsParamData.h"
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>
#import <AdSupport/ASIdentifierManager.h>

@implementation FZAdsParamData

- (NSString *)dataParam {
    NSString *model = [[UIDevice currentDevice] model];
    NSString *osVersion = [[UIDevice currentDevice] systemVersion];
    NSString *screenHeight = [NSString stringWithFormat:@"%f", [UIScreen mainScreen].bounds.size.height];
    NSString *screenWidth = [NSString stringWithFormat:@"%f", [UIScreen mainScreen].bounds.size.width];
    NSString *network = [self getNetworktype];
    NSString *imsi = [self getCarrierName];
    NSString *userAgent = [self getUserAgent];
    NSString *idfa = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
    
    NSMutableDictionary *dataParam = [[NSMutableDictionary alloc] init];
    [dataParam setValue:model forKey:@"model"];
    [dataParam setValue:[NSString stringWithFormat:@"%d", 2] forKey:@"os"];
    [dataParam setValue:osVersion forKey:@"osversion"];
    [dataParam setValue:network forKey:@"network"];
    [dataParam setValue:imsi forKey:@"imsi"];
    [dataParam setValue:idfa forKey:@"idfa"];
    [dataParam setValue:userAgent forKey:@"ua"];
    [dataParam setValue:[NSString stringWithFormat:@"%f", self.adHeight] forKey:@"height"];
    [dataParam setValue:[NSString stringWithFormat:@"%f", self.adWidth] forKey:@"width"];
    [dataParam setValue:screenHeight forKey:@"screen-height"];
    [dataParam setValue:screenWidth forKey:@"screen-width"];
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dataParam options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    NSString *clearedString = [self clearJSONString:jsonString];
    
    return clearedString;
}

- (NSString *)getNetworktype {
    NSArray *subviews = [[[[UIApplication sharedApplication] valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];
    NSNumber *dataNetworkItemView = nil;
    
    for (id subview in subviews) {
        if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
            dataNetworkItemView = subview;
            break;
        }
    }
    
    NSString *type = @"";
    switch ([[dataNetworkItemView valueForKey:@"dataNetworkType"]integerValue]) {
        case 0:
            type = @"no network";
            break;
            
        case 1:
            type = @"2G";
            break;
            
        case 2:
            type = @"3G";
            break;
            
        case 3:
            type = @"4G";
            break;
            
        case 4:
            type = @"LTE";
            break;
            
        case 5:
            type = @"Wifi";
            break;
            
        default:
            break;
    }
    
    return type;
}

- (NSString *)getCarrierName {
    CTTelephonyNetworkInfo *telephonyInfo = [[CTTelephonyNetworkInfo alloc] init];
    CTCarrier *carrier = [telephonyInfo subscriberCellularProvider];
    NSString *currentName = [carrier carrierName];
    return currentName;
}

- (NSString *)getUserAgent {
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
    NSString *secretAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
    return secretAgent;
}

- (NSString *)clearJSONString:(NSString *)jsonString {
    NSMutableString *mStr = [jsonString mutableCopy];
    [mStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:NSMakeRange(0, mStr.length)];
    [mStr replaceOccurrencesOfString:@"\\" withString:@"" options:NSLiteralSearch range:NSMakeRange(0, mStr.length)];
    return [NSString stringWithString:mStr];
}

@end
